package com.skybai.common;

import com.skybai.bean.*;
import com.skybai.mapper.ZentaoMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author sky-bai
 */
public class QueryUtil {

    private static final ZentaoMapper zentaoMapper;
    public static final int ALL_PRINT = 0;
    public static final int STORY_PRINT = 1;
    public static final int TASK_PRINT = 2;
    public static final int BUG_PRINT = 3;

    static {
        SqlSession sqlSession = SessionUtil.getSqlSession();
        zentaoMapper = sqlSession.getMapper(ZentaoMapper.class);
    }

    public static List<ZtBug> getBug(int id) {
        return zentaoMapper.queryBugByProjectId(id);
    }

    public static List<ZtTask> getTask(int id){
        return zentaoMapper.queryTaskByProjectId(id);
    }

    public static List<ZtStory> getStory(int id) {
        LinkedList<ZtStory> storyList = new LinkedList<>();
        List<ZtTask> taskList = getTask(id);
        for (ZtTask task : taskList) {
            ZtStory story = zentaoMapper.queryStoryByProjectId(task.getStory());
            storyList.add(story);
        }
        return storyList;
    }

    public static ZtUser getUser(String account){
        return zentaoMapper.queryUserByAccount(account);
    }

    public static ZtProject getProject(int id) {
        return zentaoMapper.queryProjectById(id);
    }

    public static String getUserAccount(int userId) {
        return zentaoMapper.queryUserAccountById(userId);
    }

    public static void showMember(int id,int m) {
        Set<ZtUser> userSet = new HashSet<>();
        if(m == QueryUtil.STORY_PRINT){
            List<ZtStory> storyList = QueryUtil.getStory(id);
            for (ZtStory story : storyList) {
                userSet.add(QueryUtil.getUser(story.getAssignedTo()));
            }
        }else if(m == QueryUtil.TASK_PRINT){
            List<ZtTask> taskList = QueryUtil.getTask(id);
            for (ZtTask task : taskList) {
                userSet.add(QueryUtil.getUser(task.getAssignedTo()));
            }
        }else if(m == QueryUtil.BUG_PRINT){
            List<ZtBug> bugList = QueryUtil.getBug(id);
            for (ZtBug bug : bugList) {
                userSet.add(QueryUtil.getUser(bug.getAssignedTo()));
            }
        }
        System.out.println("\n\n\tid\t\t部门\t\t账户\t\t姓名\n");
        for (ZtUser user : userSet) {
            System.out.format("\t%d\t\t%d\t\t%s\t\t%s%n", user.getId(), user.getDept(), user.getAccount(), user.getRealname());
        }
    }

}
